package org.grow.present.controller.auth;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.grow.present.entity.PageResults;
import org.grow.present.entity.QueryCondition;
import org.grow.present.entity.UserInfo;
import org.grow.present.exception.MissTargetException;
import org.grow.present.interceptor.AuthInterceptor;
import org.grow.present.mapper.UserInfoMapper;
import org.grow.present.service.FileTransfer;
import org.grow.present.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

/**
 * @Author: xwg
 * @CreateDate: 2024/6/5
 */

@Slf4j
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Value("${present.common.image-location}")
    private String location;

    @GetMapping("/currentUser")
    public UserInfo getCurrentUser() {
        UserInfo userInfo = userInfoMapper.findById(AuthInterceptor.getUID());
        return userInfo;
    }
    @GetMapping("/currentUID")
    public String currentUID() {
        return AuthInterceptor.getUID();
    }


    @Autowired
    private FileTransfer fileTransfer;
    @Autowired
    private UserInfoService userInfoService;


    //    修改昵称和头像
//  todo:  request body mistake import
    @PostMapping("/currentUser")
    public UserInfo modifyCurrentUser(@RequestBody UserInfo userInfo) throws IOException {


        String UID = AuthInterceptor.getUID();
        UserInfo db = userInfoMapper.findById(UID);
        if (db == null) {
            throw new MissTargetException("用户信息不存在");
        }
        db = userInfoService.patchModify(userInfo,db);

        return db;

    }


//    一般增删改查管理


    @GetMapping("/findById/{UID}")
    public UserInfo findById(@PathVariable("UID") String UID) {
        UserInfo userInfo = userInfoMapper.findById(UID);
        return userInfo;
    }

    @PostMapping("/query")
    public PageResults<UserInfo> query(@RequestBody QueryCondition qc) {
        Page<UserInfo> p = Page.of(qc.getPageNum(), qc.getPageSize());
        IPage<UserInfo> page = userInfoMapper.dynamicQuery(p, qc);
        return PageResults.createByIPage(page);
    }
    @PostMapping("/modify")
    public UserInfo modify(@RequestBody() UserInfo userInfo) throws IOException {
        UserInfo db = userInfoMapper.findById(userInfo.getUserId());
        if (db == null) {
            throw new MissTargetException("用户信息不存在");
        }
        db = userInfoService.patchModify(userInfo,db);;
        return db;

    }

}
